package partner

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//生成订单预支付
func PreOrderPay(order map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessPartner)
	values, sql := InsertSql(TablePfOrder, order)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//查询订单
func OrderById(id interface{}, multiOrm ...orm.Ormer) (*PfOrder, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = %v
	`, TablePfOrder, id)
	o := NewOrm(multiOrm, DBChessPartner)
	var order PfOrder
	err := o.Raw(sql).QueryRow(&order)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &order, err
}
func UpdateOrder(order map[string]interface{}, id interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessPartner)
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TablePfOrder, order, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
